package com.zhenai.rocyuan.commons.uitls;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class AnnontationUtils {

	public static <T extends Annotation> T getFromMethed(
			Class<T> annotationClazz, Object o, String methodName) {
		if (o == null) {
			return null;
		}
		Class<? extends Object> objectClass = o.getClass();
		try {
			Method method = objectClass.getDeclaredMethod(methodName);
			return method.getAnnotation(annotationClazz);
		} catch (SecurityException e) {
			throw new RuntimeException(e);
		} catch (NoSuchMethodException e) {
			throw new RuntimeException(e);
		}
	}

	public static <T extends Annotation> T getFromMethedOrType(
			Class<T> annotationClazz, Object o, String methodName) {
		T annotation = getFromMethed(annotationClazz, o, methodName);
		if (annotation != null) {
			return annotation;
		}
		Class<? extends Object> objectClass = o.getClass();
		annotation = objectClass.getAnnotation(annotationClazz);
		if (annotation != null) {
			return annotation;
		}
		return objectClass.getSuperclass().getAnnotation(annotationClazz); 
	}

}
